<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        ol,
        li {
            list-style: none;
        }
        
        .wp {
            width: 500px;
            height: 300px;
            border: 1px solid #ccc;
            overflow: hidden;
            margin: 100px;
            position: relative;
        }
        
        ul {
            width: 5000px;
            height: 100%;
        }
        
        ul>li {
            float: left;
            height: 100%;
            width: 500px;
            font-size: 100px;
            line-height: 300px;
            text-align: center;
            font-weight: bold;
        }
        
        ul>li.one {
            background: red;
        }
        
        ul>li.two {
            background: yellow;
        }
        
        ul>li.three {
            background: orange;
        }
        
        ul>li.four {
            background: pink;
        }
        
        ul>li.five {
            background: yellowgreen;
        }
        
        ol {
            position: absolute;
            bottom: 20px;
            left: 50%;
            margin-left: -75px;
        }
        
        ol>li {
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: purple;
        }
        
        .nav {
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -20px;
            height: 40px;
            width: 40px;
            background-color: brown;
        }
        
        .nav.right {
            left: auto;
            right: 0;
        }
        
        .on {
            background-color: #999;
        }
    </style>
</head>

<body>
    <div class="wp">
        <ul>
            <li class="one">1</li>
            <li class="two">2</li>
            <li class="three">3</li>
            <li class="four">4</li>
            <li class="five">5</li>
        </ul>
        <ol>
            <li class="on"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <div class="nav left"></div>
        <div class="nav right"></div>
    </div>
    <script>
        let ul = document.querySelector("ul");
        let lis = document.querySelectorAll("ul>li");
        let liWidth = ul.querySelector("li").clientWidth;
        let rightBtn = document.querySelector(".right");
        let leftBtn = document.querySelector(".left");
        let oli = document.querySelectorAll("ol>li");
        let num = 0;
        let timer;
        lis.forEach((item, index) => {
            item.dataset.index = index;
        })


        leftBtn.onclick = function() {
            num = -liWidth;
            ul.style.marginLeft = -liWidth + "px";
            let last = document.querySelector("ul>li:last-child");
            let first = document.querySelector("ul>li:first-child");
            ul.insertBefore(last, first);
            timer = setInterval(function() {
                num += 10;
                if (num > 0) {
                    clearInterval(timer);
                    return;
                }
                ul.style.marginLeft = num + "px";
            }, 100)
        }
        rightBtn.onclick = function() {
            timer = setInterval(function() {
                let last = document.querySelector("ul>li:last-child");
                let first = document.querySelector("ul>li:first-child");
                num -= 10;
                if (num < -liWidth) {
                    clearInterval(timer);
                    ul.appendChild(first);
                    ul.style.marginLeft = 0 + "px";
                    num = 0;
                    return;
                }
                ul.style.marginLeft = num + "px";
            }, 100)
        }

        oli.forEach((item, index) => {
            item.onclick = function() {
                console.log(1)
                let newIndex = index;
                let oldIndex = parseInt(document.querySelector("ul>li:first-child").getAttribute("data-index"));
                let cloneFirst = document.querySelector("ul>li:first-child").cloneNode(true);
                oli.forEach(item => {
                    item.className = "";
                })
                this.className = "on";

                if (newIndex > oldIndex) {
                    for (var i = 0; i < newIndex - oldIndex; i++) {
                        ul.appendChild(document.querySelector("ul>li:first-child"))
                    }
                    ul.insertBefore(cloneFirst, document.querySelector("ul>li:first-child"))
                    timer = setInterval(function() {
                        num += 10;
                        if (num > liWidth) {
                            clearInterval(timer)
                            num = 0;
                            document.querySelector("ul>li:nth-child(1)").remove();
                            ul.style.marginLeft = 0 + "px"
                            timer = undefined;
                            return;

                        }
                        ul.style.marginLeft = -num + "px";
                    }, 10)
                } else if (newIndex < oldIndex) {

                    for (var i = 0; i < oldIndex - newIndex; i++) {
                        ul.insertBefore(document.querySelector("ul>li:last-child"), document.querySelector("ul>li:first-child"))
                    }
                    document.querySelector("ul>li:first-child").after(cloneFirst)

                    ul.style.marginLeft = -liWidth + "px";
                    num = -liWidth;
                    timer = setInterval(function() {
                        num += 10;
                        if (num >= 0) {
                            clearInterval(timer)
                            document.querySelector('ul>li:first-child').nextElementSibling.remove();
                            return;
                        }
                        ul.style.marginLeft = num + "px";


                    }, 1000)

                    // document.querySelector("ul>li:first-child").remove()
                }
            }


        })
    </script>
</body>

</html>